from math import factorial

hash = {}
for i in xrange(0, 10):
    hash[str(i)] = factorial(i)

count = 1
while True:
    if hash['9'] * count < 10**count:
        limit = count - 1 
        break
    count += 1

s = 0
for i in xrange(10, 10**limit):
    if sum([hash[x] for x in str(i)]) == i:
        s += i   
print s
